﻿using System;
using System.Collections.Generic;
using System.Drawing;

namespace ProCP.Sim
{
    [Serializable()]
    class Cross
    {
        //for Drawing
        private const int laneWidthScalar = 3; //%;
        Rectangle rCross;
         [NonSerialized()]
        Brush brushAsphalt = Brushes.Black;
         [NonSerialized()]
        System.Drawing.Drawing2D.HatchBrush aHatchBrush;
         [NonSerialized()]
        Brush brushGrass = Brushes.DarkGreen;
        Rectangle[] rGrass;
        private Rectangle[][] lights;
         [NonSerialized()]
        Pen penCentralLine = Pens.White;
        public Point[] pointCentralLines; //raod[0] -point 0,1; road[1] point 2,3;     road[2] point 4,5;      road[3] point 6,7
        Point[] pointCentralDotedLines;
        private Rectangle[] ZebreCrossings;
        [NonSerialized()]
        Brush l;
        //logic
        public Road[] roads;
        private int CrossType;
        private int[][][] TraffightStateOrder; //[state][light][road,lane]
        //[state],[number of traffic lights],[] 
        private int trafficLightState = 0;
        private int remainingGreenTime;
        private DateTime lightTimer;
        int roadWidth; // to draw doted lines 
        [NonSerialized()] public List<Car> carsInCrossing; // all cars in the middle of crossing
        public Cross(Point corner,Size scalar,int type)
        {
            carsInCrossing = new List<Car>();
            aHatchBrush = new
            System.Drawing.Drawing2D.HatchBrush(System.Drawing.
            Drawing2D.HatchStyle.DarkVertical, Color.White, Color.Black);
            rCross = new Rectangle(corner, scalar);
            CType = type;
            //in the road car is 4/6 width of the road
            //so there is gap of 1/6 then car 4/6 and gap of 1/6
            //like so 
            //  lane    1   2   3   4   5   6
            //          |   _____________   |
            //          |   |            |  |
            //          |   |            |  |
            //          |   |    Car     |  |
            //          |   |            |  |
            //          |   |            |  |
            //          |   |____________|  |
                        
            //          ______LaneWidth________
            
            //1/6 of lane width
            //dont know how to call it laneOffSet???????
            var laneWidth = scalar.Width * laneWidthScalar / 100 ; //laneWidth in px
            var laneOffSet = laneWidth/ 6;       
            switch (type) 
            {
                case 1:
                    TraffightStateOrder = new int[4][][];//4 number of states 

                    TraffightStateOrder[0]= new int[2][];//2 lights for state 0
                    TraffightStateOrder[0][0] = new int[2] { 0, 1 };
                    TraffightStateOrder[0][1] = new int[2] { 2, 1 };

                    TraffightStateOrder[1]= new int[2][];//2 lights for state 1
                    TraffightStateOrder[1][0] = new int[2] { 0, 0 };
                    TraffightStateOrder[1][1] = new int[2] { 2, 0 };
                                        
                    TraffightStateOrder[2]= new int[2][];//2 lights for state 2
                    TraffightStateOrder[2][0] = new int[2] { 1, 1 };
                    TraffightStateOrder[2][1] = new int[2] { 3, 1 };

                    TraffightStateOrder[3]= new int[2][];//2 lights for state 3
                    TraffightStateOrder[3][0] = new int[2] { 1, 0 };
                    TraffightStateOrder[3][1] = new int[2] { 3, 0 };


                    lightTimer = DateTime.Now;

                    roads = new Road[4];
                    roads[0] = new Road(laneWidth, 2, 1);
                    roads[1] = new Road(laneWidth, 2, 1);
                    roads[2] = new Road(laneWidth, 2, 1);
                    roads[3] = new Road(laneWidth, 2, 1);


                    //grass
                    rGrass = new Rectangle[4];
                    rGrass[0] = new Rectangle(
                        corner.X,
                        corner.Y,
                        scalar.Width / 2 -  (laneWidth* roads[0].GetNrOfInLanes()),
                        scalar.Height / 2 - (laneWidth* roads[3].GetNrOfOutLanes() ));
                    rGrass[1] = new Rectangle(
                        corner.X + scalar.Width / 2 + (laneWidth* roads[0].GetNrOfOutLanes()),
                        corner.Y,
                        scalar.Width / 2 - (laneWidth* roads[0].GetNrOfOutLanes()),
                        scalar.Height / 2 - (laneWidth* roads[1].GetNrOfInLanes()));
                    rGrass[2] = new Rectangle(
                        corner.X + scalar.Width / 2+ (laneWidth * roads[0].GetNrOfInLanes()),
                        corner.Y + scalar.Height / 2 + (laneWidth * roads[3].GetNrOfOutLanes()),
                        scalar.Width / 2 - (laneWidth * roads[0].GetNrOfInLanes()),
                        scalar.Height / 2 - (laneWidth * roads[3].GetNrOfOutLanes()));
                    rGrass[3] = new Rectangle(
                        corner.X ,
                        corner.Y+scalar.Height/2 +(laneWidth * roads[3].GetNrOfInLanes()),
                        scalar.Width / 2 - (laneWidth * roads[2].GetNrOfOutLanes()),
                        scalar.Height / 2 - (laneWidth * roads[3].GetNrOfInLanes()));

                    // central lines points
                    /* 
                    *              0
                    * 
                    *              1
                    *      6   7       3   2
                    * 
                    *              5
                    * 
                    *              4
                    */

                    pointCentralLines = new Point[8];
                    pointCentralLines[0] = new Point(corner.X + scalar.Width / 2, corner.Y);
                    pointCentralLines[1] = new Point(pointCentralLines[0].X, rGrass[1].Bottom);

                    pointCentralLines[2] = new Point(corner.X+scalar.Width, corner.Y+scalar.Height/2);
                    pointCentralLines[3] = new Point(rGrass[2].X, corner.Y + scalar.Width / 2);

                    pointCentralLines[4] = new Point(corner.X + scalar.Width / 2, corner.Y+scalar.Height);
                    pointCentralLines[5] = new Point(pointCentralLines[4].X, rGrass[3].Y);

                    pointCentralLines[6] = new Point(corner.X, corner.Y+scalar.Height/2);
                    pointCentralLines[7] = new Point(rGrass[0].Right, corner.Y+scalar.Height/2);


                    //**
                    roadWidth = laneWidth * 3;
                    //
                    pointCentralDotedLines = new Point[8];
                    pointCentralDotedLines[0] = new Point(pointCentralLines[0].X - roadWidth / 3, pointCentralLines[0].Y  );
                    pointCentralDotedLines[1] = new Point(pointCentralLines[1].X - roadWidth / 3, (pointCentralLines[1].Y - roadWidth / 3)+laneWidth);

                    pointCentralDotedLines[2] = new Point(pointCentralLines[2].X - roadWidth / 3, pointCentralLines[2].Y - roadWidth / 3);
                    pointCentralDotedLines[3] = new Point((pointCentralLines[3].X - roadWidth / 3)+laneWidth, pointCentralLines[3].Y - roadWidth / 3);

                    pointCentralDotedLines[4] = new Point(pointCentralLines[4].X + roadWidth / 3, pointCentralLines[4].Y + roadWidth / 3);
                    pointCentralDotedLines[5] = new Point(pointCentralLines[5].X + roadWidth / 3, (pointCentralLines[5].Y + roadWidth / 3)-laneWidth);

                    pointCentralDotedLines[6] = new Point(pointCentralLines[6].X , pointCentralLines[6].Y + roadWidth / 3);
                    pointCentralDotedLines[7] = new Point((pointCentralLines[7].X + roadWidth / 3)-laneWidth, pointCentralLines[7].Y + roadWidth / 3);

                    lights = new Rectangle[4][];
                    lights[0] = new Rectangle[2] 
                        {new Rectangle(pointCentralLines[1].X - 4*laneWidth,pointCentralLines[1].Y-7*laneOffSet, 2*laneWidth,2*laneWidth),
                         new Rectangle(pointCentralLines[1].X - 6*laneWidth-1,pointCentralLines[1].Y-7*laneOffSet, 2*laneWidth,2*laneWidth)};
                    lights[1] = new Rectangle[2]
                         {new Rectangle(pointCentralLines[3].X-laneWidth, pointCentralLines[3].Y-4*laneWidth, 2*laneWidth,2*laneWidth),
                         new Rectangle(pointCentralLines[3].X -laneWidth,pointCentralLines[3].Y-(6*laneWidth)-1, 2*laneWidth,2*laneWidth)};
                    lights[2] = new Rectangle[2]
                         {new Rectangle(pointCentralLines[5].X+laneWidth*2,pointCentralLines[5].Y-laneWidth, 2*laneWidth,2*laneWidth),
                         new Rectangle((pointCentralLines[5].X + (6*laneWidth)-laneWidth*2)+1,pointCentralLines[5].Y-laneWidth, 2*laneWidth,2*laneWidth)};
                    lights[3] = new Rectangle[2]                        
                        {new Rectangle(pointCentralLines[7].X -laneWidth ,pointCentralLines[7].Y+ 2*laneWidth, 2*laneWidth,2*laneWidth),
                         new Rectangle(pointCentralLines[7].X -laneWidth ,(pointCentralLines[7].Y+ 4*laneWidth)+1, 2*laneWidth,2*laneWidth)};

                    //
                    roads[0].addLaneIn(
                        3,
                        new Point(pointCentralLines[0].X - 5 * laneOffSet, pointCentralLines[0].Y),
                        new Point(pointCentralLines[0].X - 5 * laneOffSet, pointCentralLines[1].Y));
                    roads[0].addLaneIn(
                        3,
                        new Point(pointCentralLines[0].X - 11 * laneOffSet, pointCentralLines[0].Y),
                        new Point(pointCentralLines[0].X - 11 * laneOffSet, pointCentralLines[1].Y));
                    roads[0].addlaneOut(
                        new Point(pointCentralLines[1].X + laneOffSet, pointCentralLines[1].Y),
                        new Point(pointCentralLines[0].X + laneOffSet, pointCentralLines[0].Y));
                     roads[1].addLaneIn(
                        0,
                        new Point(pointCentralLines[2].X, pointCentralLines[2].Y - 5 * laneOffSet),
                        new Point(pointCentralLines[3].X, pointCentralLines[3].Y - 5 * laneOffSet));
                   
                    roads[1].addLaneIn(
                        3,
                        new Point(pointCentralLines[2].X, pointCentralLines[2].Y - 11 * laneOffSet),
                        new Point(pointCentralLines[3].X, pointCentralLines[3].Y - 11 * laneOffSet));
                    roads[1].addlaneOut(
                        new Point(pointCentralLines[3].X-laneWidth, pointCentralLines[3].Y + laneOffSet),
                        new Point(pointCentralLines[2].X-laneWidth, pointCentralLines[2].Y + laneOffSet));

                    roads[2].addLaneIn(
                        0,
                        new Point(pointCentralLines[4].X + laneOffSet, pointCentralLines[4].Y),
                        new Point(pointCentralLines[5].X + laneOffSet, pointCentralLines[5].Y));
                    roads[2].addLaneIn(
                        3,
                        new Point(pointCentralLines[4].X + 7 * laneOffSet, pointCentralLines[4].Y),
                        new Point(pointCentralLines[5].X + 7 * laneOffSet, pointCentralLines[5].Y));
                    roads[2].addlaneOut(
                        new Point(pointCentralLines[5].X - 5 * laneOffSet, pointCentralLines[5].Y-laneWidth),
                        new Point(pointCentralLines[4].X - 5 * laneOffSet, pointCentralLines[4].Y-laneWidth));

                    roads[3].addLaneIn(
                        0,
                        new Point(pointCentralLines[6].X, pointCentralLines[6].Y + laneOffSet),
                        new Point(pointCentralLines[7].X, pointCentralLines[7].Y + laneOffSet));
                    roads[3].addLaneIn(
                        3,
                        new Point(pointCentralLines[6].X, pointCentralLines[6].Y + 7 * laneOffSet),
                        new Point(pointCentralLines[7].X, pointCentralLines[7].Y + 7 * laneOffSet));
                   roads[3].addlaneOut(
                        new Point(pointCentralLines[7].X, pointCentralLines[7].Y - 5 * laneOffSet),
                        new Point(pointCentralLines[6].X, pointCentralLines[6].Y - 5 * laneOffSet));
                    break;

                case 2:
                    TraffightStateOrder = new int[3][][];//4 number of states 

                    TraffightStateOrder[0]= new int[2][];//2 lights for state 1
                    TraffightStateOrder[0][0] = new int[2] { 0, 0 };
                    TraffightStateOrder[0][1] = new int[2] { 2, 0 };
                                        
                    TraffightStateOrder[1]= new int[2][];//2 lights for state 2
                    TraffightStateOrder[1][0] = new int[2] { 1, 1 };
                    TraffightStateOrder[1][1] = new int[2] { 3, 1 };

                    TraffightStateOrder[2]= new int[2][];//2 lights for state 3
                    TraffightStateOrder[2][0] = new int[2] { 1, 0 };
                    TraffightStateOrder[2][1] = new int[2] { 3, 0 };


                    lightTimer = DateTime.Now;

                    roads = new Road[4];
                    roads[0] = new Road(laneWidth, 1, 1);
                    roads[1] = new Road(laneWidth, 2, 1 );
                    roads[2] = new Road(laneWidth, 1, 1 );
                    roads[3] = new Road(laneWidth, 2, 1 );


                    //grass
                    rGrass = new Rectangle[4];
                    rGrass[0] = new Rectangle(
                        corner.X,
                        corner.Y,
                        scalar.Width / 2 -  (laneWidth* roads[0].GetNrOfInLanes()),
                        scalar.Height / 2 - (laneWidth* roads[3].GetNrOfOutLanes() ));
                    rGrass[1] = new Rectangle(
                        corner.X + scalar.Width / 2 + (laneWidth* roads[0].GetNrOfOutLanes()),
                        corner.Y,
                        scalar.Width / 2 - (laneWidth* roads[0].GetNrOfOutLanes()),
                        scalar.Height / 2 - (laneWidth* roads[1].GetNrOfInLanes()));
                    rGrass[2] = new Rectangle(
                        corner.X + scalar.Width / 2+ (laneWidth * roads[0].GetNrOfInLanes()),
                        corner.Y + scalar.Height / 2 + (laneWidth * roads[3].GetNrOfOutLanes()),
                        scalar.Width / 2 - (laneWidth * roads[0].GetNrOfInLanes()),
                        scalar.Height / 2 - (laneWidth * roads[3].GetNrOfOutLanes()));
                    rGrass[3] = new Rectangle(
                        corner.X ,
                        corner.Y+scalar.Height/2 +(laneWidth * roads[3].GetNrOfInLanes()),
                        scalar.Width / 2 - (laneWidth * roads[2].GetNrOfOutLanes()),
                        scalar.Height / 2 - (laneWidth * roads[3].GetNrOfInLanes()));



                    pointCentralLines = new Point[8];
                    pointCentralLines[0] = new Point(corner.X + scalar.Width / 2, corner.Y);
                    pointCentralLines[1] = new Point(pointCentralLines[0].X, rGrass[1].Bottom);

                    pointCentralLines[2] = new Point(corner.X+scalar.Width, corner.Y+scalar.Height/2);
                    pointCentralLines[3] = new Point(rGrass[2].X, corner.Y + scalar.Width / 2);

                    pointCentralLines[4] = new Point(corner.X + scalar.Width / 2, corner.Y+scalar.Height);
                    pointCentralLines[5] = new Point(pointCentralLines[4].X, rGrass[3].Y);

                    pointCentralLines[6] = new Point(corner.X, corner.Y+scalar.Height/2);
                    pointCentralLines[7] = new Point(rGrass[0].Right, corner.Y+scalar.Height/2);


                    lights = new Rectangle[4][];
                     lights[0] = new Rectangle[1] 
                        {new Rectangle((pointCentralLines[1].X - 3*laneWidth)-laneOffSet,(pointCentralLines[1].Y-laneWidth)-laneOffSet, 2*laneWidth,2*laneWidth),};
                       
                    lights[1] = new Rectangle[2]
                         {new Rectangle(pointCentralLines[3].X+laneOffSet, pointCentralLines[3].Y-4*laneWidth, 2*laneWidth,2*laneWidth),
                         new Rectangle(pointCentralLines[3].X +laneOffSet,pointCentralLines[3].Y-(6*laneWidth)-1, 2*laneWidth,2*laneWidth)};
                    lights[2] = new Rectangle[1]
                         {new Rectangle((pointCentralLines[5].X+laneWidth)+laneOffSet,(pointCentralLines[5].Y-laneWidth)+laneOffSet, 2*laneWidth,2*laneWidth),};
                        
                    lights[3] = new Rectangle[2]                        
                        {new Rectangle(pointCentralLines[7].X -laneWidth*2 ,pointCentralLines[7].Y+ 2*laneWidth, 2*laneWidth,2*laneWidth),
                         new Rectangle(pointCentralLines[7].X -laneWidth*2 ,(pointCentralLines[7].Y+ 4*laneWidth)+1, 2*laneWidth,2*laneWidth)};
                    //**
                    roadWidth = laneWidth * 3;
                    //
                    pointCentralDotedLines = new Point[4];
                   
                    pointCentralDotedLines[0] = new Point(pointCentralLines[2].X - roadWidth / 3, pointCentralLines[2].Y - roadWidth / 3);
                    pointCentralDotedLines[1] = new Point((pointCentralLines[3].X - roadWidth / 3)+laneWidth, pointCentralLines[3].Y - roadWidth / 3);

                    pointCentralDotedLines[2] = new Point(pointCentralLines[6].X , pointCentralLines[6].Y + roadWidth / 3);
                    pointCentralDotedLines[3] = new Point((pointCentralLines[7].X + roadWidth / 3)-laneWidth, pointCentralLines[7].Y + roadWidth / 3);

                    //
                     ZebreCrossings = new Rectangle[2];
                    //+laneWidth * roads[0].GetNrOfInLanes()
                    ZebreCrossings[0] = new Rectangle(
                        pointCentralLines[1].X-roads[0].GetNrOfInLanes()*laneWidth ,
                        pointCentralLines[1].Y -(laneWidth*2)+laneOffSet,
                        ((roads[0].GetNrOfInLanes()+roads[0].GetNrOfOutLanes()) * laneWidth),
                        10
                        
                        );
                    ZebreCrossings[1] = new Rectangle(
                        pointCentralLines[5].X - roads[0].GetNrOfInLanes() * laneWidth,
                        pointCentralLines[5].Y-laneOffSet ,
                        ((roads[0].GetNrOfInLanes() + roads[0].GetNrOfOutLanes()) * laneWidth),
                        10

                       );
                    //
                    roads[0].addLaneIn(
                        3,
                        new Point(pointCentralLines[0].X - 5 * laneOffSet, pointCentralLines[0].Y),
                        new Point(pointCentralLines[0].X - 5 * laneOffSet, pointCentralLines[1].Y));
                    roads[0].addlaneOut(
                        new Point(pointCentralLines[1].X + laneOffSet, pointCentralLines[1].Y),
                        new Point(pointCentralLines[0].X + laneOffSet, pointCentralLines[0].Y));
                   
                    roads[1].addLaneIn(
                        4,
                        new Point(pointCentralLines[2].X, pointCentralLines[2].Y - 5 * laneOffSet),
                        new Point(pointCentralLines[3].X, pointCentralLines[3].Y - 5 * laneOffSet));
                    roads[1].addLaneIn(
                        1,
                        new Point(pointCentralLines[2].X, pointCentralLines[2].Y - 11 * laneOffSet),
                        new Point(pointCentralLines[3].X, pointCentralLines[3].Y - 11 * laneOffSet));
                    roads[1].addlaneOut(
                        new Point(pointCentralLines[3].X-laneWidth, pointCentralLines[3].Y + laneOffSet),
                        new Point(pointCentralLines[2].X-laneWidth, pointCentralLines[2].Y + laneOffSet));

                    roads[2].addLaneIn(
                        3,
                        new Point(pointCentralLines[4].X + laneOffSet, pointCentralLines[4].Y),
                        new Point(pointCentralLines[5].X + laneOffSet, pointCentralLines[5].Y));
                    roads[2].addlaneOut(
                        new Point(pointCentralLines[5].X - 5 * laneOffSet, pointCentralLines[5].Y-laneWidth),
                        new Point(pointCentralLines[4].X - 5 * laneOffSet, pointCentralLines[4].Y-laneWidth));

                    roads[3].addLaneIn(
                        4,
                        new Point(pointCentralLines[6].X, pointCentralLines[6].Y + laneOffSet),
                        new Point(pointCentralLines[7].X-laneOffSet*2, pointCentralLines[7].Y + laneOffSet));
                    roads[3].addLaneIn(
                        1,
                        new Point(pointCentralLines[6].X, pointCentralLines[6].Y + 7 * laneOffSet),
                        new Point(pointCentralLines[7].X - laneOffSet * 2, pointCentralLines[7].Y + 7 * laneOffSet));
                   roads[3].addlaneOut(
                        new Point(pointCentralLines[7].X, pointCentralLines[7].Y - 5 * laneOffSet),
                        new Point(pointCentralLines[6].X, pointCentralLines[6].Y - 5 * laneOffSet));
                    break;

            }
        }

        public int CType { get; private set; }

        public void RemoveAllCars()
        {
            for (int i = 0; i < roads.Length; i++)
            {
                for (int x = 0; x < roads[i].lanesIn.Length; x++)
                {
                    roads[i].lanesIn[x].carsOnLane.Clear();
                }
                for (int y = 0; y < roads[i].lanesOut.Length; y++)
                {
                    roads[i].lanesOut[y].carsOnLane.Clear();
                }
                    
            }
        }

        public bool InterestsWithCarsInCross(RectangleF r)
        {
            foreach (Car c in carsInCrossing)
            {
                if (c.ToDraw.IntersectsWith(r))
                {
                    return true;
                }
            }
            return false;
        }

        public void SimluateLights()
        {
          if (lightTimer<DateTime.Now)
            {
                //TraffightStateOrder
                if (remainingGreenTime > 0)
                {
                    remainingGreenTime --;
                }
                else
                {
                     for (int i = 0;i<TraffightStateOrder.Length;i++)
                    {
                        for (int j = 0; j < roads[i].lanesIn.Length; j++)
                        {
                            roads[i].lanesIn[j].TrafficLightValue = 0;
                        }
                    }
                    //int biggest = 0; if can set dif green time for sate state
                    //two green lights at the time
                    //trafficl1
                    roads[TraffightStateOrder[trafficLightState][0][0]].lanesIn[
                        TraffightStateOrder[trafficLightState][0][1]].TrafficLightValue = 1;
                    //traficl2
                    roads[TraffightStateOrder[trafficLightState][1][0]].lanesIn[
                        TraffightStateOrder[trafficLightState][1][1]].TrafficLightValue = 1;
                    //reset green time 
                    remainingGreenTime =
                        roads[TraffightStateOrder[trafficLightState][0][0]].lanesIn[
                            TraffightStateOrder[trafficLightState][0][1]].GreenTime;
                    //update state
                    trafficLightState = (trafficLightState + 1)%TraffightStateOrder.Length;
                }
                lightTimer = DateTime.Now.AddSeconds(1);
            }
        }

        public void Draw(Graphics g)
        {
           
            g.FillRectangle(brushAsphalt,rCross);
            g.FillRectangles(brushGrass, rGrass);
            if (this.CType == 2)
            {
                g.FillRectangles(aHatchBrush, ZebreCrossings);
            }
            float[] dashValues = { 5, 2, 6, 4 };
             
            Pen WhiteDotedPen = new Pen(Color.White, 1);
            WhiteDotedPen.DashPattern = dashValues;
            for (var i = 0; i < pointCentralLines.Length/2; i++)
            {

                g.DrawLine(penCentralLine,pointCentralLines[2*i],pointCentralLines[2*i+1]);
            }
            for (int i = 0; i < pointCentralDotedLines.Length / 2; i++)
            {
                g.DrawLine(WhiteDotedPen, pointCentralDotedLines[2 * i], pointCentralDotedLines[2 * i + 1]);
            }

            for (int i = 0; i < roads.Length; i++)
            {
                for (int j = 0; j < roads[i].lanesIn.Length; j++)
                {
                     
                     l = Brushes.DarkRed;
                    if (roads[i].lanesIn[j].TrafficLightValue == 1)
                    {
                        l = Brushes.GreenYellow;
                    }
                    g.FillRectangle(l,lights[i][j]);

                }
            }

        }

        public void DrawCars(Graphics g)
        {

            foreach (var r in roads)
            {
                for (var index = 0; index < r.lanesIn.Length; index++)
                {
                    var laneIn = r.lanesIn[index];
                    for (var i = 0; i < laneIn.carsOnLane.Count; i++)
                    {
                        var c = laneIn.carsOnLane[i];

                        c.Draw(g);

                    }
                }

                for (var index = 0; index < r.lanesOut.Length; index++)
                {
                    var laneOut = r.lanesOut[index];
                    for (var i = 0; i < laneOut.carsOnLane.Count; i++)
                    {
                        var c = laneOut.carsOnLane[i];

                        c.Draw(g);

                    }
                }

            }
        }
    }
}
